#Set up

#s1
rm(list=ls())

setwd ("C:/Users/cecidy/Google Drive/ZH_analysis/")

source("ZHfunctions.R")
options(scipen=9999)

ZeroHunger <- read.csv("Data/ZHdf/ZeroHungerDF_2020.csv",header=TRUE, sep=",")

#s2
################################################################################################################

#start1_

#ZH and MPI00 core sample

###############################################################################################################################

#1. Prepare each dataframe - just for simplicity I keep running with the Poverty04 name

#Get rural municipalities
Poverty04 <- subset(ZeroHunger, PopDensity20002000Analysis<=150)

#######################################################################################################

#Get list of vars
load(file = "Data/ForSamples/MPI00list.rda")
load(file = "Data/ForSamples/Basic00.rda")


RuralPoverty04 <- subset(Poverty04, select=c("ZHPerCapTotal00to10for2000Analysis1000",Basic00,MPI00List))
names(RuralPoverty04)

RuralPoverty04 <- na.omit(RuralPoverty04)

#Then take away the ones affected by rural credit
load(file = "Data/ForSamples/EcludeForRCProb00.rda")

RuralPoverty04 <- subset(RuralPoverty04,(!(IBGECode7digit %in% EcludeForRCProb00)))

#Just copy to get the "quality" dataset
RuralPoverty04quality <- RuralPoverty04

#############################################################################################

#Make logged variables

#First make MPI with the correct 0s
test <- NrOfZeroALL(RuralPoverty04quality)#This gives me all the vars with 0s
MPIadd <- test[[1]]
Otheradd <- test[[2]]

#For the MPI because Ive not previously renamed when adding I just override the vars with 0 with the minimum/2
WithAdded <- lapply(MPIadd,function(y) AddConstant(RuralPoverty04quality,y,addName=""))
WithAdded <- data.frame(do.call("cbind",WithAdded))

RuralPoverty04quality[,MPIadd] <- WithAdded[,MPIadd]#just override

#Then make the MPI - use the 00 function
RuralPoverty04quality <- MakeMPI00(RuralPoverty04quality)

#######################################################################################################################

#Then for the others I cbind them on, and because Ive not included any of previous noCero there no duplicates
RuralPoverty04quality <- cbind(RuralPoverty04quality,lapply(Otheradd,function(y) AddConstant(RuralPoverty04quality,y,addName="nocero")))#

###########################################################################################################################

#And finally I need to log variables - get a list of all names

OtheraddCero <- paste(Otheradd,"nocero",sep="")#here which are names no cero

#All these need log
cols.log <- c("ZHPerCapTotal00to10for2000Analysis1000","MPI2000for2000Analysis","MPI2010for2000Analysis",
              "GDPPerCapPublicrealN2000for2000Analysis1000",OtheraddCero,"RemoteMinPopFifty2000Analysis",
              "PopDensity20002000Analysis","SizeKm22000Analysis","MeanSlopefor2000analysis","MeanElevationfor2000analysis")
addlog <- function(x) log10(x)
Log10variables <- data.frame(sapply(RuralPoverty04quality[cols.log],addlog))
colnames(Log10variables) <- paste(colnames(Log10variables), "log10",sep="")

#Get all onto ZH
RuralPoverty04quality <- cbind(RuralPoverty04quality,Log10variables)
head(RuralPoverty04quality)

###########################################################################################################################

#Take away the redundandt
Log10variables <- NULL
Poverty04 <- NULL
WithAdded <- NULL
ZeroHunger <- NULL

#############################################################################################

#I make it on log here but that I might have to go back and change after checking for log nocero or not!
RuralPoverty04quality<- getDataFrame(RuralPoverty04quality,"contr.Sum",
                                     contrastVar1="stateName",contrastVar2="Biomefor2000Analysis",setRefToMostCommonLevel,
                                     BaseVar="ZHPerCapTotal00to10for2000Analysis1000log10",
                                     MainName="ZHPerCapTotal00to10for2000Analysis1000log10scaledMain")


#fin1_

###############################################################################################

#start2_

#Prepare vectors of variables for regressions

#############################################################################################################

Depvar <- "MPI2010for2000Analysislog10"
DepVarLog <- "MPI2010for2000Analysislog10"

IndepVarLin <- "ZHPerCapTotal00to10for2000Analysis1000log10scaledMain"#already scaled so dont need to do it again

baselineDepVar <- "scale(MPI2000for2000Analysislog10)"

stateName <- "stateName"

intLin <- c("ZHPerCapTotal00to10for2000Analysis1000log10scaledMain","stateName")
intLin <- paste(intLin,collapse="*")

xvars <- c("scale(GDPPerCapPublicrealN2000for2000Analysis1000log10)",
           "scale(TotalKcal2000percapperdaynocerolog10)",
           "scale(TotalHectareCrops2000for2000Analysisnocerolog10)","scale(TotalHectarePasture2006for2000Analysisnocerolog10)",
           "scale(TotalHectareFarmsLess502006for2000Analysisnocerolog10)","scale(RemoteMinPopFifty2000Analysislog10)",
           "scale(TotRCpcNOPRONAF00to10for2000Analysis1000nocerolog10)",
           "scale(ChangeCummulativeSPEI98and00to08and10for2000Analysis)",
           "scale(MeanElevationfor2000analysislog10)","scale(MeanSlopefor2000analysislog10)",
           "scale(PopDensity20002000Analysislog10)","scale(SizeKm22000Analysislog10)",
           "Biomefor2000Analysis")

#For CBPS models
cbpsDepVar <- "ZHPerCapTotal00to10for2000Analysis1000log10"
cbpsbaselineDepVar <- "MPI2000for2000Analysislog10"

cbpsVars <- c("GDPPerCapPublicrealN2000for2000Analysis1000log10","TotalKcal2000percapperdaynocerolog10",
              "TotalHectareCrops2000for2000Analysisnocerolog10","TotalHectarePasture2006for2000Analysisnocerolog10",
              "TotalHectareFarmsLess502006for2000Analysisnocerolog10","RemoteMinPopFifty2000Analysislog10",
              "TotRCpcNOPRONAF00to10for2000Analysis1000nocerolog10",
              "ChangeCummulativeSPEI98and00to08and10for2000Analysis",
              "MeanElevationfor2000analysislog10","MeanSlopefor2000analysislog10",
              "PopDensity20002000Analysislog10","SizeKm22000Analysislog10",
              "stateName","Biomefor2000Analysis")

#########################################################################################################################

#fin2_

#########################################################################################################################

